﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Micro.Extensions.DDD.Commands;
using Micro.Extensions.DDD.Events;

namespace Micro.Extensions.DDD.Bus
{
    /// <summary>
    /// 实现我们的中介内存总线
    /// </summary>
    public class MediatorHandler : IMediatorHandler
    {
        //构造函数注入
        private readonly IMediator _mediator;


        public MediatorHandler(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 实现我们在IMediatorHandler中定义的接口
        /// 没有返回值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="command"></param>
        /// <returns></returns>
        public virtual Task<bool> SendCommand<T>(T command) where T : Command
        {
            //请求/响应模式
            return _mediator.Send(command);
        }

        /// <summary>
        /// 发布事件的实现方法
        /// </summary>
        /// <typeparam name="T">泛型 继承 Event：INotification</typeparam>
        /// <param name="event">事件模型，比如RegisteredEvent</param>
        /// <returns></returns>
        public virtual Task PublishEvent<T>(T @event) where T : Event
        {
            // MediatR中介者模式中的第二种方法，发布/订阅模式
            return _mediator.Publish(@event);
        }


    }
}
